<template>
  <div class="login-container">
    <swiper class="swiper" :options="swiperOption">
      <swiper-slide>
        <img src="../../assets/images/bg_6 (2).jpg" alt="" />
      </swiper-slide>
      <swiper-slide>
        <img src="../../assets/images/bg_6 (1).png" alt="" />
      </swiper-slide>
      <swiper-slide>
        <img src="../../assets/images/bg_2 (4).jpg" alt="" />
      </swiper-slide>
      <swiper-slide>
        <img src="../../assets/images/bg_1.jpg" alt="" />
      </swiper-slide>
      <swiper-slide>
        <img src="../../assets/images/bg_3.jpg" alt="" />
      </swiper-slide>
    </swiper>
    <el-form
        ref="loginForm"
      :model="loginForm"
      class="login-form"
      :rules="loginRules"
      auto-complete="on"
      label-position="left"
    >
      <div class="titile-container">
        <h3 class="title">请登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
        <div>账号:admin 密码:123456</div>
    </el-form>
  
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
  name: 'Login',
  components: { Swiper, SwiperSlide },

  data () {
    // 账号的校验
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入正确的账户名'))
      } else {
        callback()
      }
    }
    // 密码的校验
    const valuidatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请至少输入6位数'))
      } else {
        callback()
      }
    }
    return {
      loginRules: {
        username: [{
          required: true,
          trigger: 'blur',
          validator: validateUsername
        }],
        password: [{
          required: true,
          trigger: 'blur',
          validator: valuidatePassword
        }]
      },
      loginForm: {
        username: '',
        password: null
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined, // 重定向
      // swiper
      swiperOption: {
        autoplay: {
          delay: 2500,
          disableOnInteraction: false
        }
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      }
    }
  },
  // 打开获取焦点
  mounted () {
    console.log(this.$route)
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 显式密码
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.foucs()
      })
    },
    // 登录
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(res => {
            this.loading = false
            // 判断当前用户是不是新用户 新用户跳转到首页 否则重定向到原来页面
            if (this.loginForm.username === localStorage.getItem('new')) {
              this.$router.push({ path: this.$route.query.redirect || '/' })
            } else {
              this.$router.push('/')
              localStorage.setItem('new', this.loginForm.username)
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('登录失败')
          return false
        }
      })
    }

  }
}
</script>
<style lang='scss'>
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.swiper {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  img {
    width: 100%;
    height: 100%;
  }
}
.titile-container {
  text-align: center;
}
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.6);
    background: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
@import "../../styles/varibales";
$bg: #2d3a4b;
$dark-gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background-size: 100% 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 400px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    z-index: 1000;
  }
}
.svg-container {
  padding: 6px 5px 6px 15px;
  color: $menuActiveText;
  vertical-align: middle;
  width: 30px;
  display: inline-block;
}
.show-pwd{
  color:$menuActiveText;
}
</style>
